package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.PermissionMapper;
import com.itheima.dao.RoleMapper;
import com.itheima.dao.UserMapper;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * @description:
 * @author: dalao
 * @date: 2020/4/14-15:45
 * @Version: 1.0.0
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public User loadUserByUsername(String username) {
//        首先查询用户
        User user = userMapper.findUserByName(username);
//        获取用户id
        Integer userId = user.getId();
//        根据用户id查询角色集合
        Set<Role> roles = roleMapper.findRoleByUserId(userId);
//        遍历角色集合查询权限
        if(roles!=null&&roles.size()>0) {
            for (Role role : roles) {
//            获取roleID
                Integer roleId = role.getId();
                Set<Permission> permissions = permissionMapper.findByRoleId(roleId);
                if(permissions!=null && permissions.size()>0){
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }
        return user;

    }
}
